/********************************************************************************
* File Name:	mainwindow.cpp
* Description:	第10章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "mainwindow.h"
#include <QSqlRecord>
#include <QModelIndex>
#include <QDebug>
#include <QPalette>
#include <QWidget>

QString snumber;//保存学生登录时的用户名
QString workerIndex; //保存工作人员登录时的用户名
QString adminIndex; //保存管理员登录的用户名

//主界面构造函数
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /*信号与槽连接，当其他界面退出时返回主界面*/
    connect(welcomeAdminUi.pbAdminExist, SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeAdminUi.pbAdminExist_2, SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeAdminUi.pbAdminExist_3, SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeStuUi.pbStuExist,SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeWorkerUi.pbWorkerExist,SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeWorkerUi.pbWorkerExist_3,SIGNAL(clicked()),this,SLOT(back()));
    connect(welcomeWorkerUi.pbWorkerExist_2,SIGNAL(clicked()),this,SLOT(back()));

    QPalette plt;
    plt.setBrush(QPalette::Window, QBrush(QPixmap("./images/22.png")));//绘画主界面背景
    this->setPalette(plt);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newWelcome() //用户登录选择
{
    int userType = ui->cbUserType->currentIndex(); //获取登录类型，学生为0，职工为1，管理员为2
    QString newComerNumber,newComerPasswd;
    newComerNumber = ui->leUerName->text(); //获取输入的用户名和密码
    newComerPasswd = ui->leKeyWord->text();

    int ok = (newComerNumber.isEmpty())||(newComerPasswd.isEmpty());
    if(ok)
    {
          QMessageBox::critical(this,tr("错误!"),tr("用户名或密码为空!"));
    }
    else
    {
        QSqlQuery query;      
        switch(userType)
        {
        case 0:
            query.exec("select * from Student where snumber='"+newComerNumber+"' and spasswd='"+newComerPasswd+"'");
            if(query.next())
            {
                snumber=newComerNumber;//记录登录学生用户名
                welcomeStuUi.WelcomeStu_load();//显示学生界面
                welcomeStuUi.show();
                this->hide();//隐藏主界面
            }
            else
            {
                QMessageBox::warning(this , tr("学生登录失败"),tr("用户名或密码错误!"));
            }
            break;
        case 1:
            query.exec("select * from worker where wnumber='"+newComerNumber+"' and wpwd='"+newComerPasswd+"'");  //有无此号
            if(query.next())
            {
                workerIndex = newComerNumber; //记录登录职工用户名
                welcomeWorkerUi.show();
                this->hide();
            }
            else //登录失败
                QMessageBox::warning(this, tr("职工登录失败"),tr("用户名或密码错误！"));
            break;
        case 2:
            query.exec("select * from admin where anumber='"+newComerNumber+"' and apwd='"+newComerPasswd+"'");  //有无此号
            if(query.next())
            {
                adminIndex = newComerPasswd; //记录登录管理员用户名
                welcomeAdminUi.show();//显示管理员界面
                this->hide(); //隐藏主界面
            }
            else //登录失败
              QMessageBox::warning(this, tr("操作admin表"),tr("无此管理员账户！"));
            break;
        };
    }

    /*清空输入内容*/
    ui->leKeyWord->clear();
    ui->leUerName->clear();
    ui->leUerName->setFocus();
}

void MainWindow::back()
{
    this->show();//重新显示主界面
}
